package com.zjitc.controller;


import com.zjitc.beans.MessageBean;
import com.zjitc.pojo.Category;
import com.zjitc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService service;

    @RequestMapping("tocategory")
    public ModelAndView tocategory(){
       ModelAndView modelAndView = new ModelAndView("/category/category");
        List<Category> list = service.adminfindAll();
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping("delServices")
    @ResponseBody
    private Object delServices(String cid){
        boolean b = service.deletebycid(cid);
        return new MessageBean(b,"");
    }

    @RequestMapping("toEditCategory")
    private ModelAndView toEditCategory(String cid, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(!checkAdmin(session)){
            modelAndView.setViewName("login");
        }else{
            modelAndView.setViewName("category/editCategory");
            Category category = service.findByCid(cid);
            modelAndView.addObject("category",category);
        }

        return modelAndView;
    }

    @RequestMapping("editCategory")
    @ResponseBody
    private Object editCategory(Category category){
        boolean b = service.update(category);
        return new MessageBean(b,"");
    }

    @RequestMapping("toAddCategory")
    public String toAddCategory(){
        return "category/addCategory";
    }

    @RequestMapping("AddCategory")
    @ResponseBody
    private Object AddCategory(Category category){
        boolean b = service.add(category);
        return new MessageBean(b,"");
    }
}
